﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///JJFD 的摘要说明
/// </summary>
public partial class JJFD
{
    public enum typeJJFD { 教师 = 1, 机构 }
    public enum IsOrNot { 否 = 0, 是 = 1 }
    public enum IsOrNotCT { 否 = 0, 是 = 1, 协商确定 = 2 }
    public static JJFD GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JJFD.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<JJFD> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JJFD.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.JJFD.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<JJFD>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.JJFD.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.JJFD.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            JJFD objJJFD = JJFD.GetByID(ID);
            if (objJJFD == null)
                return;
            objJJFD.Enable_Flag = 0;
            objJJFD.Update();
        }
    }

    public static List<JJFD> GetAllByCondition(int shengID, int shiID, int quID, string address, int jjfdtype, int sfxyct, int sfbmcs, string kfdfs, string TimeFrom, string TimeTo)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.JJFD
                        where t.Enable_Flag == 1
                        select t;
            if (shengID != 0)
            {
                query = query.Where(row => row.ProvinceID == shengID);
            }
            if (shiID != 0)
            {
                query = query.Where(row => row.CityID == shiID);
            }
            if (quID != 0)
            {
                query = query.Where(row => row.ReginIDs == quID.ToString());
            }
            if (!string.IsNullOrEmpty(address))
            {
                query = query.Where(row => row.Address.Contains(address));
            }
            // JGID为0，则是家教
            if (jjfdtype == 1)
            {
                query = query.Where(row => row.JGID == 0 );
            }
            if (jjfdtype == 2)
            {
                query = query.Where(row => row.JGID != 0 );
            }
            if (sfxyct != 999)
            {
                query = query.Where(row => row.SFXYCT == sfxyct);
            }
            if (sfbmcs != 999)
            {
                query = query.Where(row => row.BMCSID == sfbmcs);
            }
            if (!string.IsNullOrEmpty(kfdfs))
            {
                query = query.Where(row => row.KFDFS.Contains(kfdfs));
            }
            if (!string.IsNullOrEmpty(TimeFrom))
            {
                query = query.Where(row => row.JJSJFrom >= DongDong.Common.Misc.ConvertToDateTime(TimeFrom));
            }
            if (!string.IsNullOrEmpty(TimeTo))
            {
                query = query.Where(row => row.JJSJTo <= DongDong.Common.Misc.ConvertToDateTime(TimeTo));
            }
            return query.OrderByDescending(row => row.ID).ToList();
        }

    }

    public static string address { get; set; }
}